package tk.mybatis.springboot.common;

import com.alibaba.fastjson.annotation.JSONField;

/**
 * 返回Json实体类
 * {
 *  "success": true,
 *  "errorCode": 0,
 *  "message": "success",
 *  "data": {}
 *  }
 */
public class BaseResponse {

    /**
     * 0代表成功， !=0 代表失败
     */
    private Boolean success;
    private int errorCode;
    private String message;
    private Object data;
    /**
     * 异常信息栈
     */
    private String stackTrace;


    public BaseResponse() {
    }

    public BaseResponse(boolean success, int code, String msg, Object data) {
        this.success = success;
        this.errorCode = code;
        this.message = msg;
        this.data = data;
    }

    public static BaseResponse success() {
        return success("success");
    }

    public static BaseResponse success(String content) {
        return new BaseResponse(true, 0, content, null);
    }

    public static BaseResponse success(String content, Object data) {
        return new BaseResponse(true, 0, content, data);
    }

    public static BaseResponse error(String content) {
        return error(1, content);
    }

    public static BaseResponse error(int code, String content) {
        return new BaseResponse(false, code, content, null);
    }

    public static BaseResponse error(ErrorEnum error, Object o) {
        return new BaseResponse(false, error.getCode(), error.getMsg(), o);
    }

    public static BaseResponse error(ErrorEnum error) {
        return error(error, null);
    }

    public Boolean getSuccess() {
        return success;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public int getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(int errorCode) {
        this.errorCode = errorCode;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public String getStackTrace() {
        return stackTrace;
    }

    public void setStackTrace(String stackTrace) {
        this.stackTrace = stackTrace;
    }


    @JSONField(serialize = false)
    public boolean isError() {
        return getErrorCode() != 0 ? true : false;
    }


}
